package cem.agony.ui;

import javafx.scene.Parent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Polygon;

public class RoadPoint extends Parent
{
    private final Line line;
    private final Polygon arrow = new Polygon( new double[]
    {
        0.0, 5.0, -5.0, -5.0, 5.0, -5.0
    } );

    public RoadPoint( double fromX, double fromY, double toX, double toY )
    {
        line = new Line( fromX, fromY, toX, toY );
        line.setStroke( Color.GRAY );
        line.setStrokeWidth( 2 );

        arrow.setStroke( Color.GRAY );
        arrow.setFill( Color.GRAY );

        arrow.setTranslateX( toX );
        arrow.setTranslateY( toY );

        rotateArrow();
        setScaleX( 0.65 );
        setScaleY( 0.65 );

        getChildren().addAll( line, arrow );
    }

    public void updateFromStation( double x, double y )
    {
        line.setStartX( x );
        line.setStartY( y );

        rotateArrow();
    }

    public void updateToStation( double x, double y )
    {
        line.setEndX( x );
        line.setEndY( y );

        arrow.setTranslateX( x );
        arrow.setTranslateY( y );

        rotateArrow();
    }

    private void rotateArrow()
    {
        double angle = Math.atan2( line.getEndY() - line.getStartY(),
                                   line.getEndX() - line.getStartX() ) * 180 / 3.14;
        arrow.setRotate( ( angle - 90 ) );
    }
}